define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'store/goods/index' + location.search,
                    add_url: 'store/goods/add',
                    edit_url: 'store/goods/edit',
                    del_url: 'store/goods/del',
                    multi_url: 'store/goods/multi',
                    import_url: 'store/goods/import',
                    table: 'store_goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'join_merchant_id', title: __('Join_merchant_id')},
                        {
                            field: 'gallery_images',
                            title: __('Gallery_images'),
                            operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.images
                        },
                        {
                            field: 'is_on_sale',
                            title: __('Is_on_sale'),
                            searchList: {"1": __('Is_on_sale 1'), "0": __('Is_on_sale 0')},
                            formatter: Table.api.formatter.normal
                        },
                        {
                            field: 'goods_image',
                            title: __('Goods_image'),
                            operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.image
                        },
                        {
                            field: 'guarantee',
                            title: __('Guarantee'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {field: 'sort_order', title: __('Sort_order')},

                        {field: 'counter_price', title: __('Counter_price'), operate: 'BETWEEN'},
                        {field: 'retail_price', title: __('Retail_price'), operate: 'BETWEEN'},
                        {field: 'details_content', title: __('Details_content')},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: '添加规格',
                                    text: '添加规格',
                                    icon: 'fa fa-plus',
                                    title: '添加规格',
                                    classname: 'btn btn-success btn-xs',
                                    url: function (row) {
                                        console.log(row);
                                        return 'goods/sku?id='+row.id;
                                    },
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
